package org.ntu.csn.as.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ntu.csn.as.manager.DBManager;
import org.ntu.csn.as.model.Category;

/**
 * Servlet implementation class TopicSelect
 */
@WebServlet("/TopicListController")
public class TopicListController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TopicListController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("TopicListController...");
		DBManager dbm = new DBManager();
		List<Category> clist = null;
		try {
			clist = dbm.getCategoryAll();
			request.setAttribute("categoryList", clist);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(Category c:clist){
			System.out.println("Topic Name:"+c.getTopic());
		}
		request.getRequestDispatcher("topicSelect.jsp").forward(request,
				response);
	}

}
